package com.itheima.reggie.common;

import com.alibaba.fastjson.JSON;
import com.itheima.reggie.utils.BaseContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 */
public class LoginLoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取session数据 判断是否为空
        Long empId = (Long) request.getSession().getAttribute("employee");

        if(empId != null){
            //存储员工id
            BaseContext.set(empId);
            return true;
        }

        Long userId = (Long) request.getSession().getAttribute("userId");

        if(userId != null){
            //存储员工id
            BaseContext.set(userId);
            return true;
        }
        //响应前端数据
        R r = R.error("NOTLOGIN");
        response.getWriter().write(JSON.toJSONString(r));
        return false;
    }
}
